#pragma once
#include "DataDef.h"

#define ERROR_KILL(hr, msg) if(!ErrorHandler::handleError(hr, msg)){ exit(0);}
#define ERRORBUF_KILL(hr, buf) if(!ErrorHandler::handleError(hr, buf)){ exit(0);}

#define SHOW_ERROR_MSG(hr, msg) {if(FAILED(hr)){MessageBox(NULL, DXGetErrorDescriptionW(hr), msg, MB_OK);}}

class ErrorHandler
{
public:
	ErrorHandler(void);
	~ErrorHandler(void);

	static BOOLEAN handleError(HRESULT hr, LPCWSTR error)
	{
		if(FAILED(hr))
		{
			MessageBox(0, error, 0, 0);
			return false;
		}
		return true;
	}

	static BOOLEAN handleError(HRESULT hr, ID3DXBuffer *errorBuffer)
	{
		if(FAILED(hr))
		{
			// output any error messages
			if(errorBuffer != NULL)
			{
				const UINT size = MultiByteToWideChar(CP_ACP, 0, (char *)errorBuffer->GetBufferPointer(), -1, NULL, 0);
				wchar_t *pwsz = new wchar_t[size];
				MultiByteToWideChar(CP_ACP, 0, (char *)errorBuffer->GetBufferPointer(), -1, pwsz, size);
				LPCWSTR wideFileName = pwsz;
				MessageBox(0, pwsz, 0, 0);
				errorBuffer->Release();
			}
			return false;
		}
		return true;
	}
};
